<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class VoteHistoryBase extends BaseItemBase {
	//user id
	var $_userid = 0;
	//survey id
	var $_sid = 0;
	//interval time
	var $_interval = 86400;
	//
	var $_ip_address = '';
	
	//constructor
	function VoteHistoryBase() {
		parent::BaseItemBase();
		
		$this->_table_owner_usable = false;
		
		$this->table = 'pp_votes_history';
		$this->prefix = 'vh_';
		
		$this->search_fields = array('username');
		$this->sort_fields = array('sort_order+', 'id+');
		
		//
		$this->_ip_address = util_get_ip_address();
	}
	
	//setter/getter
	function setUserID($userid) {
		$this->_userid = $userid;
	}
	
	function setSurveyID($sid) {
		$this->_sid = $sid;
	}
	
	//methods
	function onAddItemFilterBind($data_array) {
		$fb = array();
		if( $this->_sid > 0 ) {
			$fb['_sid'] = $this->_sid;
		}
		if( $this->_userid > 0 ) {
			$fb['_userid'] = $this->_userid;
		}
		$fb['_time'] = time();
		$fb['_ip_address'] = $this->_ip_address;
		
		return $fb;
	}
	
	//
	function checkVoted() {
		$time = time() - $this->_interval;
		$query = "select vh_id from " . $this->table . " where vh_time > " . $time . " and vh_ip_address = '" . $this->_ip_address . "'";
		$row = $this->adb->getRow($query);
		if( $row['vh_id'] > 0 ) {
			return false;
		}
		
		return true;
	}
}
//
?>